/***************************************************************
Copyright Statement:

This software/firmware and related documentation (EcoNet Software) 
are protected under relevant copyright laws. The information contained herein 
is confidential and proprietary to EcoNet (HK) Limited (EcoNet) and/or 
its licensors. Without the prior written permission of EcoNet and/or its licensors, 
any reproduction, modification, use or disclosure of EcoNet Software, and 
information contained herein, in whole or in part, shall be strictly prohibited.

EcoNet (HK) Limited  EcoNet. ALL RIGHTS RESERVED.

BY OPENING OR USING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY 
ACKNOWLEDGES AND AGREES THAT THE SOFTWARE/FIRMWARE AND ITS 
DOCUMENTATIONS (ECONET SOFTWARE) RECEIVED FROM ECONET 
AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON AN AS IS 
BASIS ONLY. ECONET EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, 
WHETHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 
OR NON-INFRINGEMENT. NOR DOES ECONET PROVIDE ANY WARRANTY 
WHATSOEVER WITH RESPECT TO THE SOFTWARE OF ANY THIRD PARTIES WHICH 
MAY BE USED BY, INCORPORATED IN, OR SUPPLIED WITH THE ECONET SOFTWARE. 
RECEIVER AGREES TO LOOK ONLY TO SUCH THIRD PARTIES FOR ANY AND ALL 
WARRANTY CLAIMS RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES 
THAT IT IS RECEIVERS SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD 
PARTY ALL PROPER LICENSES CONTAINED IN ECONET SOFTWARE.

ECONET SHALL NOT BE RESPONSIBLE FOR ANY ECONET SOFTWARE RELEASES 
MADE TO RECEIVERS SPECIFICATION OR CONFORMING TO A PARTICULAR 
STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND 
ECONET'S ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE ECONET 
SOFTWARE RELEASED HEREUNDER SHALL BE, AT ECONET'S SOLE OPTION, TO 
REVISE OR REPLACE THE ECONET SOFTWARE AT ISSUE OR REFUND ANY SOFTWARE 
LICENSE FEES OR SERVICE CHARGES PAID BY RECEIVER TO ECONET FOR SUCH 
ECONET SOFTWARE.
***************************************************************/

/************************************************************************
*                  I N C L U D E S
*************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include "omci_adpt_common.h"
#include "omci_adpt_general.h"
#include "omci_me_external.h"

#include "libcompileoption.h"
#include "libtcapi.h"
#include "cfg_api.h"

#include "hal_itf_wan/hal_itf_wan.h"

/************************************************************************
*                  D E F I N E S   &   C O N S T A N T S
*************************************************************************
*/


/************************************************************************
*                  M A C R O S
*************************************************************************
*/    
#define WAN_OPT_SET					(0)
#define WAN_OPT_GET					(1)

/************************************************************************
*                  D A T A     T Y P E S
*************************************************************************
*/

PON_OltTypeInfo_t oltTypeInfoList[] = 
{
	{"FIBERHOME_551601",  PON_OLT_FIBERHOME_551601},
	{"DASAN", PON_OLT_DASAN},
};


typedef struct WAN_ATTR_INFO_s
{
	uint8_t 	type;
    char	name[MAX_WAN_ATTR_LENTH];
}WAN_ATTR_INFO_t;


/************************************************************************
*                  E X T E R N A L     D A T A   D E C L A R A T I O N S
*************************************************************************
*/



/************************************************************************
*                  F U N C T I O N   D E C L A R A T I O N S
*************************************************************************
*/

/************************************************************************
*                  P U B L I C   D A T A
*************************************************************************
*/


/************************************************************************
*                  P R I V A T E   D A T A
*************************************************************************
*/

static WAN_ATTR_INFO_t g_wan_info[] = 
{
	{E_WAN_IF_TYPE_IP_ADDR, 	"IP"},
	{E_WAN_IF_TYPE_NETMASK, 	"NetMask"},
	{E_WAN_IF_TYPE_GATEWAY, 	"GateWay"},
	{E_WAN_IF_TYPE_PRI_DNS, 	"DNS"},
	{E_WAN_IF_TYPE_SEC_DNS, 	"SecDNS"},
	{E_WAN_IF_TYPE_MAC_ADDR,	"MacAddr"},   
	{E_WAN_IF_TYPE_ISP, 		"ISP"},
	{E_WAN_IF_TYPE_VLAN_ID, 	"VLANID"},
	{E_WAN_IF_TYPE_DOT1P,		"dot1pData"},
	{E_WAN_IF_TYPE_LINK_MODE,	"LinkMode"},
	{E_WAN_IF_TYPE_SRV_LIST,	"ServiceList"},
};
	

static WAN_ATTR_INFO_t g_wan_PPPoE_info[] = 
{
    {E_WAN_IF_TYPE_ENCAP ,          "ENCAP"},
    {E_WAN_IF_TYPE_LINK_MODE,       "LinkMode"},
    {E_WAN_IF_TYPE_PPPGETIP,        "PPPGETIP"},
    {E_WAN_IF_TYPE_CONNECTION,      "CONNECTION"},
    {E_WAN_IF_TYPE_USERNAME,        "USERNAME"},
    {E_WAN_IF_TYPE_PASSWORD,        "PASSWORD"},
    {E_WAN_IF_TYPE_ISP,             "ISP"},
    {E_WAN_IF_TYPE_NAT_ENABLE,      "NATENABLE"},
    {E_WAN_IF_TYPE_DHCP_ENABLE,     "DHCPEnable"},
    {E_WAN_IF_TYPE_PPPUNIT,         "PPPUNIT"},
    {E_WAN_IF_TYPE_IFNAME,          "IFName"},
    {E_WAN_IF_TYPE_CONNECTIONERROR, "ConnectionError"},
    {E_WAN_IF_TYPE_CLOSEIFIDLE,     "CLOSEIFIDLE"},
    {E_WAN_IF_TYPE_AUTHEN,          "AUTHEN"},
    {E_WAN_IF_TYPE_SRV_LIST,        "ServiceList"},
    {E_WAN_IF_TYPE_SRVNAME,         "SRVNAME"},
};

#define GET_WAN_ITF_INFO(id) 	g_wan_info[id]
#define GET_PPPOE_ITF_INFO(id) 	g_wan_PPPoE_info[id]

#define WAN_CREATE_BY_OMCI "Yes"
/************************************************************************
*                  F U N C T I O N   D E F I N I T I O N S
*************************************************************************
*/

int omci_tcapi_set(char *node,char * attr, char *value)
{
	return tcapi_set(node, attr, value);
}
int omci_tcapi_nget(char* node, char* attr, char *retval, int retval_len)
{
	return tcapi_nget(node, attr, retval, retval_len);
}

int omci_tcapi_commit(char *node)
{	
	return tcapi_commit(node);
}

int omci_tcapi_save(void)
{	
	return tcapi_save();
}

int omci_adpt_set_emergency(uint8_t value)
{
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "Enter [%s][%d].\n",__FUNCTION__,__LINE__);

	if(0 == value){
		tcapi_set("GPON_ONU","EmergencyState","0");
	}else if(1 == value){
		tcapi_set("GPON_ONU","EmergencyState","1");
	}
	tcapi_save();
	
	return 0;
}


int omci_adpt_get_olt_type(void)
{
	int index = 0;
	char oltType[20]={0};
	char strBuf[100]={0};

	tcapi_nget("XPON_Common", "oltType", oltType,sizeof(oltType));
	if(strcmp(oltType,"") != 0){
		for(index = 0 ; index < sizeof(oltTypeInfoList)/sizeof(oltTypeInfoList[0]) ;index++){
			if(strcasecmp(oltType,oltTypeInfoList[index].oltName) == 0){
				oltTypeId = oltTypeInfoList[index].oltId;
				omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN, "\r\n find olt id=%d name=%s index=%d\r\n", oltTypeId,oltTypeInfoList[index].oltName,oltTypeInfoList[index].oltId);
			}
		}
	}
	else
		omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN, "\r\n not get olt name from config \r\n");
	blapi_pon_set_olt_type(oltTypeId);
	omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN, "\r\n system(%s) \r\n",strBuf);

	tcapi_set("OMCI_Entry", "Ready", "start");

	return 0;
}


/*OMCI Upgrade*/

int omci_image_get_version(char *version, uint8_t ver_len, uint8_t image_flag)
{
	char ver[15] = {0};

	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "Enter [%s][%d].\n",__FUNCTION__,__LINE__);
	if((NULL == version)||(ver_len < 14)){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]Parameter is Null.\n",__FUNCTION__,__LINE__);
		return -1;
	}
	if(0 == image_flag){
		tcapi_nget("GPON_SoftImage0", "Version", ver,sizeof(ver));
	}else {
		tcapi_nget("GPON_SoftImage1", "Version", ver,sizeof(ver));
	}
	memcpy(version,ver,ver_len);

    return 0;
}
int omci_image_get_bootflag(uint8_t * pbootflag)
{
	uint8_t buffer[64] = {0};
	FILE *fp= NULL;

	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "[%s][%d] Enter .\n",__FUNCTION__,__LINE__);
	if(NULL == pbootflag){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]Parameter is Null.\n",__FUNCTION__,__LINE__);
		return -1;
	}
	if(TCSUPPORT_C1_MS_VAL || TCSUPPORT_CHS_VAL)
	{
		fp = fopen("proc/tc3162/gpon_bootflag", "r");
		if(fp)
		{
			memset(buffer, 0, sizeof(buffer));
			fgets((char *)buffer, sizeof(buffer), fp);
			omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[%s][%s][%d]gpon_boot: buffer =%s\n",__FILE__,__FUNCTION__, __LINE__,buffer) ;
			if(strncmp((char *)buffer,"1", 1) == 0)
			{
				*pbootflag = 1;
			}
			else
			{
				*pbootflag = 0;
			}
			fclose(fp);
		}
		else
		{
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"gpon_boot:---fopen proc/tc3162/gpon_bootflag fail!\n") ;		
			return -1;
		}
	}
	else{
		if(blapi_pon_get_boot_flag(pbootflag) != 0)
		{
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"gpon_boot:---fopen /tmp/image_boot_flag fail!\n") ;	
			return -1;
		}
	}
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "[%s][%d], return 0, bootflag=%d\n",__FUNCTION__,__LINE__,*pbootflag);
    return 0;
}
int omci_image_set_bootflag(uint8_t bootflag)
{
	return blapi_pon_set_bootFlag_to_flash(bootflag);
}
int omci_image_save_state(softimage_state_t *softimage_state)
{
	char attr_val[4] = {0};
	char tmp[15] = {0};
	int i = 0;
    struct img_state{
        uint8_t commit;
        uint8_t active;
        uint8_t valid;
    };
    static struct  omciSoftwareImageState{
        struct img_state img0;
        struct img_state img1;
        char value[64];/*software image state type*/
    }mode_table[] = {
        /*  {image0_commit  image0_active    image0_valid 	},{   image1_commit    image1_active   image1_valid	},		 "state"   */ 
        {{COMMITTED_IMAGE,   ACTIVE_IMAGE,   VALID_IMAGE,   },{UNCOMMITTED_IMAGE, INACTIVE_IMAGE, INVALID_IMAGE	},"SOFTWARE_IMAGE_STATE_S1_0"},
        {{COMMITTED_IMAGE,   ACTIVE_IMAGE,   VALID_IMAGE,   },{UNCOMMITTED_IMAGE, INACTIVE_IMAGE, VALID_IMAGE	},"SOFTWARE_IMAGE_STATE_S3_0"},
        {{COMMITTED_IMAGE,   INACTIVE_IMAGE, VALID_IMAGE,   },{UNCOMMITTED_IMAGE, ACTIVE_IMAGE,   VALID_IMAGE	},"SOFTWARE_IMAGE_STATE_S4_0"},
        {{UNCOMMITTED_IMAGE, ACTIVE_IMAGE, 	 VALID_IMAGE,   },{COMMITTED_IMAGE,   INACTIVE_IMAGE, VALID_IMAGE	},"SOFTWARE_IMAGE_STATE_S4_1"},
        {{UNCOMMITTED_IMAGE, INACTIVE_IMAGE, VALID_IMAGE,   },{COMMITTED_IMAGE,   ACTIVE_IMAGE,   VALID_IMAGE	},"SOFTWARE_IMAGE_STATE_S3_1"},
        {{UNCOMMITTED_IMAGE, INACTIVE_IMAGE, INVALID_IMAGE, },{COMMITTED_IMAGE,   ACTIVE_IMAGE,   VALID_IMAGE	},"SOFTWARE_IMAGE_STATE_S1_1"},
    };

	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "Enter [%s][%d].\n",__FUNCTION__,__LINE__);
	if(NULL == softimage_state){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]Parameter is Null.\n",__FUNCTION__,__LINE__);
		return -1;
	}
	memcpy(tmp,softimage_state->image0.version,14);
	tcapi_set("GPON_SoftImage0", "Version", tmp);
	sprintf(attr_val,"%d",softimage_state->image0.committed);
	tcapi_set("GPON_SoftImage0", "Committed", attr_val);
	sprintf(attr_val,"%d",softimage_state->image0.active);
	tcapi_set("GPON_SoftImage0", "Active", attr_val);
	sprintf(attr_val,"%d",softimage_state->image0.valid);
	tcapi_set("GPON_SoftImage0", "Valid", attr_val);

	memcpy(tmp,softimage_state->image1.version,14);
	tcapi_set("GPON_SoftImage1", "Version", tmp);
	sprintf(attr_val,"%d",softimage_state->image1.committed);
	tcapi_set("GPON_SoftImage1", "Committed", attr_val);
	sprintf(attr_val,"%d",softimage_state->image1.active);
	tcapi_set("GPON_SoftImage1", "Active", attr_val);
	sprintf(attr_val,"%d",softimage_state->image1.valid);
	tcapi_set("GPON_SoftImage1", "Valid", attr_val);
	
    omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"omci_image_save_state: image0_commit = %d image0_active = %d image0_valid = %d image1_commit = %d image1_active = %d image1_valid = %d \n",
        softimage_state->image0.committed,softimage_state->image0.active,softimage_state->image0.valid,
        softimage_state->image1.committed,softimage_state->image1.active,softimage_state->image1.valid);

    for (i = 0 ; i < sizeof(mode_table)/sizeof(struct  omciSoftwareImageState); i ++)
    {
        if (softimage_state->image0.committed == mode_table[i].img0.commit	&& 
			softimage_state->image0.active == mode_table[i].img0.active 	&& 
			softimage_state->image0.valid == mode_table[i].img0.valid   	&&
			softimage_state->image1.committed == mode_table[i].img1.commit	&&
			softimage_state->image1.active == mode_table[i].img1.active 	&& 
			softimage_state->image1.valid == mode_table[i].img1.valid)
        {
            tcapi_set(GPONCOMMONNODE,OMCI_SOFTWAREIMAGE_STATE,mode_table[i].value);
            omcidbgPrintf(OMCI_DEBUG_LEVEL_DEBUG,"========== %d %s =========\r\n",__LINE__,mode_table[i].value);
            goto end;
        }

    }

    tcapi_set(GPONCOMMONNODE,OMCI_SOFTWAREIMAGE_STATE,"SOFTWARE_IMAGE_STATE_S0");
    omcidbgPrintf(OMCI_DEBUG_LEVEL_DEBUG,"========== %d %s =========\r\n",__LINE__,"SOFTWARE_IMAGE_STATE_S0");
end:
	tcapi_commit("GPON_SoftImage0");
	tcapi_commit("GPON_SoftImage1");
    tcapi_commit(GPONCOMMONNODE);	
	tcapi_save();
    return 0;
}

int omci_image_read_state(softimage_state_t *softimage_state)
{
	char attr_val[4] = {0};
    uint8_t buffer[64] = {0};
    uint8_t bootflag = 0;

	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "[%s][%d] Enter .\n",__FUNCTION__,__LINE__);
	if(NULL == softimage_state){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]Parameter is Null.\n",__FUNCTION__,__LINE__);
		return -1;
	}

	tcapi_nget(GPONCOMMONNODE,OMCI_SOFTWAREIMAGE_STATE,(char *)buffer,sizeof(buffer));
    if((0 == strncmp((char *)buffer, "SOFTWARE_IMAGE_STATE_S4_0", strlen("SOFTWARE_IMAGE_STATE_S4_0")) ) ||
        (0 == strncmp((char *)buffer, "SOFTWARE_IMAGE_STATE_S4_1", strlen("SOFTWARE_IMAGE_STATE_S4_1")) )){

		tcapi_nget("GPON_SoftImage0", "Committed", attr_val,sizeof(attr_val));
		softimage_state->image0.committed = (uint8_t)atoi(attr_val);
		tcapi_nget("GPON_SoftImage0", "Active", attr_val,sizeof(attr_val));
		softimage_state->image0.active = (uint8_t)atoi(attr_val);
		tcapi_nget("GPON_SoftImage0", "Valid", attr_val,sizeof(attr_val));
		softimage_state->image0.valid = (uint8_t)atoi(attr_val);
		
		tcapi_nget("GPON_SoftImage1", "Committed", attr_val,sizeof(attr_val));
		softimage_state->image1.committed = (uint8_t)atoi(attr_val);
		tcapi_nget("GPON_SoftImage1", "Active", attr_val,sizeof(attr_val));
		softimage_state->image1.active = (uint8_t)atoi(attr_val);
		tcapi_nget("GPON_SoftImage1", "Valid", attr_val,sizeof(attr_val));
		softimage_state->image1.valid = (uint8_t)atoi(attr_val);
		
    }else {
		omci_image_get_bootflag(&bootflag);
		if(bootflag == 0){
			softimage_state->image0.committed = 1;
			softimage_state->image0.active = 1;
			softimage_state->image0.valid = 1;
			softimage_state->image1.committed = 0;
			softimage_state->image1.active = 0;
			tcapi_nget("GPON_SoftImage1", "Valid", attr_val,sizeof(attr_val));
			softimage_state->image1.valid = (uint8_t)atoi(attr_val);
		}else{
			softimage_state->image0.committed = 0;
			softimage_state->image0.active = 0;
			tcapi_nget("GPON_SoftImage0", "Valid", attr_val,sizeof(attr_val));
			softimage_state->image0.valid = (uint8_t)atoi(attr_val);
			softimage_state->image1.committed = 1;
			softimage_state->image1.active = 1;
			softimage_state->image1.valid = 1;
		}
		
		sprintf(attr_val,"%d",softimage_state->image0.committed);
		tcapi_set("GPON_SoftImage0", "Committed", attr_val);
		sprintf(attr_val,"%d",softimage_state->image0.active);
		tcapi_set("GPON_SoftImage0", "Active", attr_val);
		sprintf(attr_val,"%d",softimage_state->image0.valid);
		tcapi_set("GPON_SoftImage0", "Valid", attr_val);
		
		sprintf(attr_val,"%d",softimage_state->image1.committed);
		tcapi_set("GPON_SoftImage1", "Committed", attr_val);
		sprintf(attr_val,"%d",softimage_state->image1.active);
		tcapi_set("GPON_SoftImage1", "Active", attr_val);
		sprintf(attr_val,"%d",softimage_state->image1.valid);
		tcapi_set("GPON_SoftImage1", "Valid", attr_val);

		tcapi_commit("GPON_SoftImage0");
		tcapi_commit("GPON_SoftImage1");
    }

	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "----------------softImage0----------\n");
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "committed=%d,active=%d,valid=%d\n",softimage_state->image0.committed,softimage_state->image0.active,softimage_state->image0.valid);
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "----------------softImage1----------\n");
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "committed=%d,active=%d,valid=%d\n",softimage_state->image1.committed,softimage_state->image1.active,softimage_state->image1.valid);

    return 0;
}

void convert_to_pppoe_attr(int type, char* buf, char* attr)
{
    
    uint16_t length = 0;
    int pvcIdx= 0;
    int entryIdx = 0;
    if(NULL == buf || NULL == attr){
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "convert_to_pppoe_attr input error\n");
                return;
    }

	switch(type)
    {
        case E_WAN_IF_TYPE_USERNAME:
            if(strlen(omciPPPoEInfo.username) != 0)
            {
                strncpy(attr,omciPPPoEInfo.username,MAX_WAN_ATTR_LENTH);
                return;
            }else if(omci_adpt_get_one_string_from_inst(OMCI_CLASS_ID_LARGE_STRING, omciPPPoEInfo.userNameStrTP, buf, &length))
        	{
        		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "get zte 250 ME userName str ptr fail\n");
                return;
        	}
            strncpy(attr,buf,MAX_WAN_ATTR_LENTH);
            break;
        case E_WAN_IF_TYPE_SRVNAME:
            if(omci_adpt_get_one_string_from_inst(OMCI_CLASS_ID_LARGE_STRING, omciPPPoEInfo.serviceStrTP, buf, &length))
        	{
        		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "get zte 250 ME service str ptr fail\n");
                return;
        	}
            strncpy(attr,buf,MAX_WAN_ATTR_LENTH);
            break;
        case E_WAN_IF_TYPE_PPPUNIT:
            if(0 != get_wan_link_index(omciPPPoEInfo.path,&pvcIdx,&entryIdx)){
                omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "path format error.\n");
                return;
            }
            sprintf(attr, "%d",((pvcIdx)*8+entryIdx));
            break;
        case E_WAN_IF_TYPE_IFNAME:
            if(0 != get_wan_link_index(omciPPPoEInfo.path,&pvcIdx,&entryIdx)){
                omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "path format error.\n");
                return;
            }
            sprintf(attr, "PPP%d",((pvcIdx)*8+entryIdx));
            break;
        default :
            strcpy(attr,buf);
            break;
    }
}

int parsePPPoEInfo (void)
{
	char buffer[OMCI_LAYER3_STR_BUFF_SIZE] = {0};
    int tmp = 0;

    omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"====[%s][%d]enable=0x%x iphostId=0x%x ====\r\n",__FUNCTION__,
                    __LINE__,omciPPPoEInfo.enable,omciPPPoEInfo.iphostId);

    if (FALSE == omciPPPoEInfo.enable || (0xFF == omciPPPoEInfo.iphostId))
            return 0;

    if (0 == omciPPPoEInfo.NATMode)
    {
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_NATENABLE], "Disabled");
    }else{
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_NATENABLE], "Enable");
    }

    if (0 == omciPPPoEInfo.AuthVal){
        memset(g_pppoe_wan[PPPOE_IF_TYPE_AUTHEN],0,OMCI_LAYER3_STR_BUFF_SIZE);
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_AUTHEN], " ");
    }else if (1 == omciPPPoEInfo.NATMode){
        memset(g_pppoe_wan[PPPOE_IF_TYPE_AUTHEN],0,OMCI_LAYER3_STR_BUFF_SIZE);
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_AUTHEN], "CHAP");
    }else if (2 == omciPPPoEInfo.NATMode){
        memset(g_pppoe_wan[PPPOE_IF_TYPE_AUTHEN],0,OMCI_LAYER3_STR_BUFF_SIZE);
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_AUTHEN], "PAP");
    }
    if (0 == omciPPPoEInfo.connectMode)
    {
        memset(g_pppoe_wan[PPPOE_IF_TYPE_CONNECTION],0,OMCI_LAYER3_STR_BUFF_SIZE);
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_CONNECTION], "Connect_Keep_Alive");
    }else if(1 == omciPPPoEInfo.connectMode)
    {
        memset(g_pppoe_wan[PPPOE_IF_TYPE_CONNECTION],0,OMCI_LAYER3_STR_BUFF_SIZE);
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_CONNECTION], "Connect_on_Demand");
    }else if(2 == omciPPPoEInfo.connectMode)
    {
        memset(g_pppoe_wan[PPPOE_IF_TYPE_CONNECTION],0,OMCI_LAYER3_STR_BUFF_SIZE);
        strcpy(g_pppoe_wan[PPPOE_IF_TYPE_CONNECTION], "Connect_Manually");
    }

    tmp = omciPPPoEInfo.releaseTime * 60;
    memset(g_pppoe_wan[PPPOE_IF_TYPE_CLOSEIFIDLE],0,OMCI_LAYER3_STR_BUFF_SIZE);
    sprintf(buffer, "%d", tmp);
    strcpy(g_pppoe_wan[PPPOE_IF_TYPE_CLOSEIFIDLE], buffer);
    strcpy(g_pppoe_wan[PPPOE_IF_TYPE_PASSWORD], omciPPPoEInfo.password);

    memset(g_pppoe_wan[PPPOE_IF_TYPE_USERNAME],0,OMCI_LAYER3_STR_BUFF_SIZE);
    strcpy(g_pppoe_wan[PPPOE_IF_TYPE_USERNAME], omciPPPoEInfo.username);
    
    return 0;
}


int omci_set_pppoe_attr(char* path)
{
    int type = 0;
    WAN_ATTR_INFO_t wan_info			= {0};
    char * buf;
    char attr_val[MAX_WAN_ATTR_LENTH] 	= {0};
    char change;
    int ret 	= 0;

    if(NULL == path){
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[SET]OMCI omci_set_pppoe_attr input path is NULL\n");
        return -1;
    }

    parsePPPoEInfo();

    for (type = PPPOE_IF_TYPE_ENCAP; type < PPPOE_IF_TYPE_MAX; type ++){
        wan_info = GET_PPPOE_ITF_INFO(type);
        buf = g_pppoe_wan[type];
        convert_to_pppoe_attr(wan_info.type,buf,attr_val);
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[SET]OMCI omci_set_pppoe_attr type %d %s val %s to wan %s\n",wan_info.type,wan_info.name,attr_val,path);
    	ret = set_wan_link_attr(path,MAX_WAN_PATH_LENTH,wan_info.type,attr_val,&change);
        if(ret != 0)
        {
    		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"OMCI set_wan_link_attr fail return \n");
    	    return -1;	    	
        }
    }
    return ret;
}

static char* omci_wan_get_path(uint16_t IPHostIstId,uint8_t opt)
{
	int ret = 0;
    int idx = 0;
    char wanPath[MAX_WAN_PATH_LENTH]={0};
	if((strlen(g_iphost_wan[IPHostIstId].path) == 0) || (cfg_query_object(g_iphost_wan[IPHostIstId].path,NULL,NULL) <= 0))
    {//if no path, need to create wan and get path
        /* add for ME 250 */
        if( (TRUE == omciPPPoEInfo.enable ) && (IPHostIstId == omciPPPoEInfo.iphostId )
            && (0 == strlen(omciPPPoEInfo.path)) && (opt == WAN_OPT_SET)){
            if(find_wan_path_by_serv("INTERNET",wanPath)){
                memcpy(g_iphost_wan[IPHostIstId].path,wanPath,MAX_WAN_PATH_LENTH);
                memcpy(omciPPPoEInfo.path,wanPath,MAX_WAN_PATH_LENTH);
			    return g_iphost_wan[IPHostIstId].path;	
            }else{
                ret = omci_wan_create(IPHostIstId);
        	   	if(ret != 0)
        	    {
        	    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"creat_wan_link fail return \n");
        	        return NULL;
        	    }
                omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN," iphost %d new path is %s\n",IPHostIstId,g_iphost_wan[IPHostIstId].path);
                memcpy(omciPPPoEInfo.path,g_iphost_wan[IPHostIstId].path,MAX_WAN_PATH_LENTH);
			    return g_iphost_wan[IPHostIstId].path;		
            }
        }
		if(opt == WAN_OPT_GET)
        {
        	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] Iphost %d get operation don't need to create\n",__FUNCTION__,__LINE__,IPHostIstId);
            return NULL;        	
        }
        
    	idx = (svclist_is_tr69(IPHostIstId))? E_WAN_SVC_TYPE_TR069 : E_WAN_SVC_TYPE_VOICE;
        if(!(g_web_rcd_info[idx].valid) || (cfg_query_object(g_web_rcd_info[idx].path,NULL,NULL) <= 0))
        {        
	    	ret = omci_wan_create(IPHostIstId);
		   	if(ret != 0)
		    {
		    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d]creat_wan_link fail return \n",__FUNCTION__,__LINE__);
		        return NULL;
		    }
	        
	        omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[%s][%d] iphost %d new path is %s\n",__FUNCTION__,__LINE__,IPHostIstId,g_iphost_wan[IPHostIstId].path);
        }
        else 
        {			
            memcpy(g_iphost_wan[IPHostIstId].path,g_web_rcd_info[idx].path,MAX_WAN_PATH_LENTH);
        }
	}
    return g_iphost_wan[IPHostIstId].path;
}

int omci_wan_create(uint16_t IPHostIstId)
{
	int ret = 0;
    
    omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[%s][%d] iphost %d has not exist, need to create\n",__FUNCTION__,__LINE__,IPHostIstId);
    ret = creat_wan_link(g_iphost_wan[IPHostIstId].path,MAX_WAN_PATH_LENTH);
    
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[%s][%d] OMCI creat_wan_link service %s ret %s\n",__FUNCTION__,__LINE__,(IPHostIstId?"VOICE":"TR069"),((ret==0)?"Success":"Fail"));
    return ret;
}

int omci_wan_delete(uint16_t IPHostIstId)
{
	char *path 	= NULL;	
    
	OMCI_CHECK_IPHOST_ID(IPHostIstId);
	
    path = omci_wan_get_path(IPHostIstId,WAN_OPT_GET);
    if(path == NULL)
	{
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] OMCI GET wan path fail, wan not exist return \n",__FUNCTION__,__LINE__);
	    return 0;		
	}
	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] begin to del wan %s\n",__FUNCTION__,__LINE__,path);
    if(0 != del_wan_link(path))
	{
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] del_wan_link %s fail\n",__FUNCTION__,__LINE__,path);
	    return -1;		
	}    

    memset(path,0,MAX_WAN_PATH_LENTH);

    return 0;
}

int omci_wan_delete_by_type(uint32_t svc_type)
{
    wan_link_path_info_t wan_info = {0};
    char attr_val[MAX_WAN_ATTR_LENTH] 	= {0};
    uint8_t i = 0;
    int ret = 0;
    uint8_t j = 0;
    char svc_list[MAX_WAN_ATTR_LENTH] = {0};
    
	if(svc_type >= E_WAN_SVC_TYPE_MAX)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] OMCI Serice list type error \n",__FUNCTION__,__LINE__);
	    return 0;		
	}
	
    memset(svc_list,0,MAX_WAN_ATTR_LENTH);
    if ( E_WAN_SVC_TYPE_TR069 == svc_type ){
        strcpy(svc_list,"TR069");
    } else if ( E_WAN_SVC_TYPE_VOICE == svc_type ){
        strcpy(svc_list,"VOICE");
    }

    if(get_wan_link_all_entry_path(&wan_info) == 0 && wan_info.num > 0)
    {
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] get wan success wan num %d\n",__FUNCTION__,__LINE__,wan_info.num);
    	for(i = 0;i < wan_info.num;i++)
        {
        	memset(attr_val,0,MAX_WAN_ATTR_LENTH);
        	ret = get_wan_link_attr(wan_info.path[i],E_WAN_IF_TYPE_SRV_LIST,attr_val,MAX_WAN_ATTR_LENTH);
            if(0 != ret)
            {
                omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] get wan path %s OMCI svc list is error\n",__FUNCTION__,__LINE__,wan_info.path[i]);
                return 0;
            }

        	if(0 != strcmp(attr_val,svc_list))
            {
                continue;
            }

            /* clear iphost record info */
 			for(j = 0; j < MAX_IPHOST_WAN_NUM; j++)
			{
        	    omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] del wan path [%s]  iphost path[%s]\n",__FUNCTION__,__LINE__,wan_info.path[i],g_iphost_wan[j].path);

				if(0 == strcmp(g_iphost_wan[j].path, wan_info.path[i]))
				{
                    g_iphost_wan[j].change = 2;
					memset(g_iphost_wan[j].path,0,sizeof(g_iphost_wan[j].path));
				}
			}

            /* not created by omci don't delete */
            memset(attr_val,0,MAX_WAN_ATTR_LENTH);
            ret = get_wan_link_attr(wan_info.path[i],E_WAN_IF_TYPE_CREAT_OMCI,attr_val,MAX_WAN_ATTR_LENTH);
            if(0 != ret){
                omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] check omci create failed just return path[%s] attr[%s]\n",__FUNCTION__,__LINE__,wan_info.path[i],attr_val);
                return 0;
            }
            if(0 != strcmp(attr_val,WAN_CREATE_BY_OMCI)){
                omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] non omci create just return\n",__FUNCTION__,__LINE__);
                return 0;
            }

        	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] del wan path %s\n",__FUNCTION__,__LINE__,wan_info.path[i]);
        	if(0 != del_wan_link(wan_info.path[i]))
        	{
        		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] del_wan_link %s fail\n",__FUNCTION__,__LINE__,wan_info.path[i]);
        	    return -1;		
        	}   
        }  
    }

    return 0;
}

int omci_wan_clear_all(void)
{
	wan_link_path_info_t wan_info = {0};
    char attr_val[MAX_WAN_ATTR_LENTH] 	= {0};
    uint8_t i = 0;
    int ret = 0;
	uint8_t j = 0;

    if(get_wan_link_all_entry_path(&wan_info) == 0 && wan_info.num > 0)
    {
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d] get wan success wan num %d\n",__FUNCTION__,__LINE__,wan_info.num);
    	for(i = 0;i < wan_info.num;i++)
        {
        	memset(attr_val,0,MAX_WAN_ATTR_LENTH);
        	ret = get_wan_link_attr(wan_info.path[i],E_WAN_IF_TYPE_CREAT_OMCI,attr_val,MAX_WAN_ATTR_LENTH);

        	if(0 == ret)
            {

            	omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN,"[%s][%d] get wan path %s OMCI Create Flag is %s\n",__FUNCTION__,__LINE__,wan_info.path[i],attr_val);
            	if(0 == strcmp(attr_val,WAN_CREATE_BY_OMCI))
                {
                	omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN,"[%s][%d] del omci create wan path %s\n",__FUNCTION__,__LINE__,wan_info.path[i]);
                	del_wan_link(wan_info.path[i]);
					for(j = 0; j < MAX_IPHOST_WAN_NUM; j++)
					{
						if(0 == strcmp(g_iphost_wan[j].path, wan_info.path[i]))
						{
							memset(g_iphost_wan[j].path,0,sizeof(g_iphost_wan[j].path));
						}
					}
                }
            }    
        }
    }
    return 0;
}

int omci_wan_set_attr(uint16_t IPHostIstId, char* path)
{
    int ret 	= 0;
    int type = 0;
    WAN_ATTR_INFO_t wan_info			= {0};
    char attr_val[MAX_WAN_ATTR_LENTH] 	= {0};
    char * buf;
    char change;
    int web_idx = 0;
    char is_pppoe = ((ENABLE == omciPPPoEInfo.enable) && (IPHostIstId == omciPPPoEInfo.iphostId))? 1:0;

	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[Begin] %s %d iphost %d\n",__FUNCTION__,__LINE__,IPHostIstId);
    OMCI_CHECK_IPHOST_ID(IPHostIstId);
    if(0 == g_iphost_wan[IPHostIstId].change){
        return ret;
    }

	if(path == NULL)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"%s input path is NULL return \n",__FUNCTION__);
	    return -1;	    	
    }
    if(svclist_is_tr69(IPHostIstId))
        web_idx = E_WAN_SVC_TYPE_TR069;
    else
        web_idx = E_WAN_SVC_TYPE_VOICE;

    /*OLT only set ISP and ISP is set to 0, just keep the manual settings */
	if(((1<<WAN_IF_TYPE_ISP) | (1<<WAN_IF_TYPE_LINKMODE) | (1<<WAN_IF_TYPE_SVC_LIST))
		 == g_iphost_wan[IPHostIstId].attr_mask)
        return 0;

    /*If olt set ISP mode, ONU should change LinkMode to linkIP*/
    if (0 != ((1<<WAN_IF_TYPE_ISP) & g_iphost_wan[IPHostIstId].attr_mask)) {

        g_iphost_wan[IPHostIstId].attr_mask |= (1<<WAN_IF_TYPE_LINKMODE);
        strncpy(g_iphost_wan[IPHostIstId].val[WAN_IF_TYPE_LINKMODE],"linkIP",MAX_WAN_ATTR_LENTH-1);
    }

    for (type = WAN_IF_TYPE_IP_ADDR; type < WAN_IF_TYPE_MAX; type ++){
	    OMCI_CHECK_TYPE_ID(type);
        if(0 == ((g_iphost_wan[IPHostIstId].attr_mask) & (1<<type)))
            continue;

        if(is_pppoe && (WAN_IF_TYPE_SVC_LIST == type))
            continue;

        wan_info = GET_WAN_ITF_INFO(type);
        buf = g_iphost_wan[IPHostIstId].val[type];
    	omci_wan_convert_to_attr(type,buf,attr_val);
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN,"[SET]OMCI set_wan_link_attr type %d %s val %s to wan %s\n",wan_info.type,wan_info.name,attr_val,path);
    	ret = set_wan_link_attr(path,MAX_WAN_PATH_LENTH,wan_info.type,attr_val,&change);
        if(ret != 0)
        {
    		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"OMCI set_wan_link_attr fail return \n");
    	    return -1;	    	
        }
            
    	if(change == 1 && !is_pppoe)
        {
            memcpy(g_web_rcd_info[web_idx].path, path,MAX_WAN_PATH_LENTH);
            omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN,"OMCI iphost wan %d path has changed as %s,change flag %d \n",IPHostIstId,path,change);
        }
        
        omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[End] %s %d \n",__FUNCTION__,__LINE__);
    }

    if(is_pppoe) {
        omci_set_pppoe_attr(path);
    }
    else {
        omci_link_to_web(IPHostIstId,g_iphost_wan[IPHostIstId].val[WAN_IF_TYPE_SVC_LIST]);
    }
	return 0;
}

int omci_wan_get_attr(int type ,uint16_t IPHostIstId, char* buf,uint8_t buf_size)
{
	char *path 	= NULL;	
    int ret 	= 0;
    WAN_ATTR_INFO_t wan_info 			= {0};
    char attr_val[MAX_WAN_ATTR_LENTH] 	= {0};

	if(buf == NULL)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"%s input pointer buf is NULL return \n",__FUNCTION__);
	    return -1;	    	
    }
    
    OMCI_CHECK_IPHOST_ID(IPHostIstId);
    OMCI_CHECK_TYPE_ID(type);

	wan_info = GET_WAN_ITF_INFO(type);
    path = omci_wan_get_path(IPHostIstId,WAN_OPT_GET);
    if(path == NULL)
	{
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"%s %d OMCI GET wan path fail, wan not exist return \n",__FUNCTION__,__LINE__);
	    return -1;		
	}
    
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[GET]OMCI Get wan_link_attr type %d %s From wan %s\n",wan_info.type,wan_info.name,path);
    
	ret = get_wan_link_attr(path,wan_info.type,attr_val,MAX_WAN_ATTR_LENTH);
    if(ret != 0)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," OMCI GET wan_link_attr fail return \n");
	    return -1;	    	
    }

    if(strlen(attr_val) >= buf_size)
    {
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[GET]OMCI get wan_link_attr fail attr lenth %lu too long\n",strlen(attr_val));
        return -1;
    }

    strcpy(buf,attr_val);
   	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[GET]OMCI get wan_link_attr type %s val %s\n",wan_info.name,buf);
	return 0;
}

void omci_wan_dump_web_rcd(void)
{
    int i = 0;
    int j = 0;
    WAN_ATTR_INFO_t wan_info = {0};
    omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"\r\n\r\n===================now is dump web rcd info===================\r\n");
    for ( i = E_WAN_SVC_TYPE_TR069; i < E_WAN_SVC_TYPE_MAX ; i ++)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"web %s is %s  \r\n",
			((E_WAN_SVC_TYPE_TR069 == i)? "TR69" : "VOICE"),
                        ((1 == g_web_rcd_info[i].valid)?"valid":"invalid"));
        if (g_web_rcd_info[i].valid)
        {
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"==== start print info ====\r\n");
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," [path] [%s] ====\r\n",g_web_rcd_info[i].path);
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," [change] [%d] ====\r\n",g_web_rcd_info[i].change);
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," [iphostId] [%d] ====\r\n",g_web_rcd_info[i].iphostId);

            for (j = WAN_IF_TYPE_IP_ADDR; j < WAN_IF_TYPE_SVC_LIST; j ++)
            {
                wan_info = GET_WAN_ITF_INFO(j);
                omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," [%s] [%s]\r\n",wan_info.name,g_web_rcd_info[i].attr_val[j]);
            }
        }
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d]==== end print info ====\r\n\r\n",__FUNCTION__,__LINE__);
    }
    return;

}

void omci_wan_dump_iphost_wan(void)
{
    int i = 0;
    int j = 0;
    WAN_ATTR_INFO_t wan_info = {0};
    omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"\r\n\r\n===================now is dump iphost rcd info===================\r\n");

    for ( i = 0; i < MAX_IPHOST_WAN_NUM ; i ++)
    {
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"iphost %d %s change[%d]  \r\n",i,
                        ((1 == g_iphost_wan[i].change)?"set":"did not set"),g_iphost_wan[i].change);
        if (g_iphost_wan[i].change)
        {
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"==== start print info ====\r\n");
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," [path] [%s] ====\r\n",g_iphost_wan[i].path);
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," [attr_mask][0x%x]\r\n",g_iphost_wan[i].attr_mask);
            for (j = WAN_IF_TYPE_IP_ADDR; j < WAN_IF_TYPE_MAX; j ++)
            {
                if( 0 == (g_iphost_wan[i].attr_mask & (1<<j)))
                    continue;
                wan_info = GET_WAN_ITF_INFO(j);
                omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR," [%s] [%s]\r\n",wan_info.name,g_iphost_wan[i].val[j]);
            }
        }
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s][%d]==== end print info ====\r\n\r\n",__FUNCTION__,__LINE__);
    }

}

int omci_wan_init_web_rcd( void)
{
    char svc_list[MAX_WAN_ATTR_LENTH] = {0};
    int i = 0;
    int j = 0;
    WAN_ATTR_INFO_t wan_info = {0};

    memset(g_web_rcd_info,0,(E_WAN_SVC_TYPE_MAX * sizeof(OMCI_IPHOST_WEB_RCD_t)));
    for ( i = E_WAN_SVC_TYPE_TR069; i < E_WAN_SVC_TYPE_MAX ; i ++)
    {

        memset(svc_list,0,MAX_WAN_ATTR_LENTH);
        if ( E_WAN_SVC_TYPE_TR069 == i ){
            strcpy(svc_list,"TR069");
        } else if ( E_WAN_SVC_TYPE_VOICE == i ){
            strcpy(svc_list,"VOICE");
        }

        g_web_rcd_info[i].valid = find_wan_path_by_serv(svc_list,g_web_rcd_info[i].path);

        if (g_web_rcd_info[i].valid)
        {
            omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN,"==== %s %d path is %s====\n",__FUNCTION__,__LINE__,g_web_rcd_info[i].path);
            g_web_rcd_info[i].iphostId = MAX_IPHOST_WAN_NUM;

            for (j = WAN_IF_TYPE_IP_ADDR; j < WAN_IF_TYPE_MAX; j ++)
            {
                wan_info = GET_WAN_ITF_INFO(j);
                get_wan_link_attr(g_web_rcd_info[i].path, wan_info.type, g_web_rcd_info[i].attr_val[j], MAX_WAN_ATTR_LENTH);
            }
        }
    }
	omci_wan_dump_web_rcd();
	omci_wan_dump_iphost_wan();
    return 0;
}

void omci_update_wan_rcd_info(void){
	char svc_list[MAX_WAN_ATTR_LENTH] = {0};
	char valid_flag = 0;
	int i = 0, wan_idx = 0;
	WAN_ATTR_INFO_t wan_info = {0};

	for(wan_idx = E_WAN_SVC_TYPE_TR069; wan_idx < E_WAN_SVC_TYPE_MAX; wan_idx++){
		memset(svc_list,0,MAX_WAN_ATTR_LENTH);
        if ( E_WAN_SVC_TYPE_TR069 == wan_idx ){
            strcpy(svc_list,"TR069");
        } else if ( E_WAN_SVC_TYPE_VOICE == wan_idx ){
            strcpy(svc_list,"VOICE");
        }

		valid_flag = find_wan_path_by_serv(svc_list,g_web_rcd_info[wan_idx].path);
		
		if(valid_flag != g_web_rcd_info[wan_idx].valid){
			g_web_rcd_info[wan_idx].valid = valid_flag;
			if(valid_flag){
				for (i = WAN_IF_TYPE_IP_ADDR; i < WAN_IF_TYPE_MAX; i++)
				{
				    wan_info = GET_WAN_ITF_INFO(i);
				    get_wan_link_attr(g_web_rcd_info[wan_idx].path, wan_info.type, g_web_rcd_info[wan_idx].attr_val[i], MAX_WAN_ATTR_LENTH);
				}
			}else{
				 memset(g_web_rcd_info[wan_idx].path,0,sizeof(g_web_rcd_info[wan_idx].path));
				 memset(g_web_rcd_info[wan_idx].attr_val,0,sizeof(g_web_rcd_info[wan_idx].attr_val));
			}
		}
	}
	
    return ;
}

int omci_wan_recovery_web(uint32_t svc_type)
{
    uint32_t idx = 0;
    uint32_t type = 0;
    int ret = 0;
    char * attr_val =NULL;
    char * path = NULL;
    char change = 0;

    WAN_ATTR_INFO_t wan_info = {0};
    for (idx = E_WAN_SVC_TYPE_TR069; idx < E_WAN_SVC_TYPE_MAX; idx ++)
    {
        /* check svc type , same svc_type or MAX type do recovery */
        if((svc_type != idx) && (E_WAN_SVC_TYPE_MAX != svc_type))
            continue;

        /* omci configed this wan , but omci modify srvlist  */
        if( 1 == g_web_rcd_info[idx].change){
            for (type = WAN_IF_TYPE_IP_ADDR; type < WAN_IF_TYPE_MAX; type ++)
            {
        	    OMCI_CHECK_TYPE_ID(type);
                wan_info = GET_WAN_ITF_INFO(type);
                path = g_web_rcd_info[idx].path;
                attr_val = g_web_rcd_info[idx].attr_val[type];
                if(0 == strlen(attr_val))
                    continue;
            	ret = set_wan_link_attr(path,MAX_WAN_PATH_LENTH,wan_info.type,attr_val,&change);
                if(ret != 0)
                {
            		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"OMCI set_wan_link_attr fail return \n");
            	    return -1;	    	
                }
                    
            	if(change == 1)
                {
                    omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN," OMCI path has changed as %s,change flag %d \n",path,change);
                }
                
                omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE,"[End] %s %d \n",__FUNCTION__,__LINE__);
            }
        }
		wan_link_effect(path);
    }
    return ret;
}

static int omci_check_omci_create_wan(void)
{
    uint32_t idx = 0;
    uint8_t tr69_valid = 0;
    uint32_t tr69_idx = 0;
    uint8_t voice_valid = 0;
    uint32_t voice_idx = 0;
    int ret = 0;
    char change;
	
    for(idx = 0; idx < MAX_IPHOST_WAN_NUM; idx ++)
    {
        if(svclist_is_tr69(idx))
        {
            tr69_valid = 1;
            tr69_idx = idx;
        }else
        {
            voice_valid = 1;
            voice_idx = idx;
        }
    }
    for (idx = E_WAN_SVC_TYPE_TR069; idx < E_WAN_SVC_TYPE_MAX; idx ++)
    {
        if(idx == E_WAN_SVC_TYPE_TR069 && tr69_valid && 0 == g_web_rcd_info[idx].valid){
            /* Web did not have TR69, OMCI create*/
            set_wan_link_attr(g_iphost_wan[tr69_idx].path,MAX_WAN_ATTR_LENTH,E_WAN_IF_TYPE_CREAT_OMCI,WAN_CREATE_BY_OMCI,&change);
            continue;
        }
        if(idx == E_WAN_SVC_TYPE_VOICE && voice_valid && 0 == g_web_rcd_info[idx].valid){
            /* Web did not have VOICE, OMCI create*/
            set_wan_link_attr(g_iphost_wan[voice_idx].path,MAX_WAN_ATTR_LENTH,E_WAN_IF_TYPE_CREAT_OMCI,WAN_CREATE_BY_OMCI,&change);
            continue;
        }
    }
    return ret;
}

int omci_wan_set_effect(uint16_t iphostId)
{
	char* path = NULL;
    int ret = 0;   

	path = omci_wan_get_path(iphostId,WAN_OPT_SET);
	if(path == NULL)
    {
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"%s %d get path fail\n",__FUNCTION__,__LINE__);
        return -1;
    }    
    ret = omci_wan_set_attr(iphostId, path);
    if(ret != 0)
    {
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"%s %d set attr error\n",__FUNCTION__,__LINE__);
        return -1;
    }
    ret = omci_check_omci_create_wan();
    if(ret != 0)
    {
    	omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"%s %d check web wan error\n",__FUNCTION__,__LINE__);
        return -1;
    }
	omcidbgPrintf(OMCI_DEBUG_LEVEL_WARN,"%s %d commit WAN %s\n",__FUNCTION__,__LINE__,path);
    wan_link_effect(path);
    
    return 0;
}

/*VOIP Interface*/

/********************************************************************************************************
9.9.1 pptp pots uni
*********************************************************************************************************/
int omci_voip_get_admin_state(char *buf, uint8_t buf_size)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY, 0);
	return tcapi_nget(nodeName, "Enable", buf, buf_size);
}
int omci_voip_get_rx_gain(char *buf, uint8_t buf_size, uint16_t instance_id)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_ADVANCED_ENTRY, instance_id);
	return tcapi_nget(nodeName,"VoiceVolumeListen", buf, buf_size);
}
int omci_voip_get_tx_gain(char *buf, uint8_t buf_size, uint16_t instance_id)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_ADVANCED_ENTRY, instance_id);
	return tcapi_nget(nodeName,"VoiceVolumeSpeak", buf, buf_size);
}
int omci_voip_get_hook_state(char *buf, uint8_t buf_size, uint16_t instance_id)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "VoIPSysParam_Entry%d", instance_id);
	return tcapi_nget(nodeName, "SC_LINE_HOOK_STATE", buf, buf_size);
}

int omci_voip_set_admin_state(char *buf, uint16_t portId)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY, portId);
	tcapi_set(nodeName, "Enable", buf);
	return tcapi_commit(nodeName);
}
int omci_voip_set_rx_gain(char *buf, uint16_t instance_id)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_ADVANCED_ENTRY, instance_id);
	tcapi_set(nodeName,"VoiceVolumeListen", buf);
	return tcapi_commit(nodeName);
}
int omci_voip_set_tx_gain(char *buf, uint16_t instance_id)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_ADVANCED_ENTRY, instance_id);
	tcapi_set(nodeName,"VoiceVolumeSpeak", buf);
	return tcapi_commit(nodeName);
}

/********************************************************************************************************
9.9.2 sip user data
*********************************************************************************************************/
int omci_voip_get_sip_disp_name(char *buf, uint8_t buf_size, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY,index);	
    return tcapi_nget(nodeName, OMCI_SIP_DISP_NAME, buf, buf_size);
}
int omci_voip_get_voice_exp_time(char *buf, uint8_t buf_size)
{
	if(buf == NULL){
		return -1;
	}
    return tcapi_nget(OMCI_VOIP_ADVANCED_COMMON, OMCI_SIP_VOICEMAIL_EXPIRE, buf, buf_size);
}

int omci_voip_set_user_part_aor(char *buf, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	snprintf(nodeName, MAX_BUFFER_SIZE-1 ,"%s%d", OMCI_VOIP_BASIC_ENTRY,index);	
	tcapi_set(nodeName, OMCI_SIP_USER_PART_AOR, buf);
	return tcapi_commit(nodeName);
}
int omci_voip_set_sip_disp_name(char *buf, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY,index);	
	tcapi_set(nodeName, OMCI_SIP_DISP_NAME, buf);
	return tcapi_commit(nodeName);
}
int omci_voip_set_sip_user_name(char *buf, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY,index);	
	tcapi_set(nodeName, OMCI_SIP_AUTH_USER_NAME, buf);
	return tcapi_commit(nodeName);
}
int omci_voip_set_sip_password(char *buf, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY,index);	
	tcapi_set(nodeName, OMCI_SIP_PASSWORD, buf);
	return tcapi_commit(nodeName);
}
int omci_voip_set_sip_enable(char *buf, uint8_t index)/*without commit ??*/
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY, index);
    return tcapi_set(nodeName, "SIPEnable", "Yes");
}
int omci_voip_set_sip_uri(char *buf, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY,index);
	tcapi_set(nodeName, OMCI_SIP_URI, (char *)buf);
	return tcapi_commit(nodeName);
}
int omci_voip_set_voice_exp_time(char *buf)
{
	if(buf == NULL){
		return -1;
	}
	tcapi_set(OMCI_VOIP_ADVANCED_COMMON, OMCI_SIP_VOICEMAIL_EXPIRE, buf);
	return tcapi_commit(OMCI_VOIP_ADVANCED_COMMON);
}

int omci_voip_reset_sip_user_entry(uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	sprintf(nodeName, "%s%d", OMCI_VOIP_BASIC_ENTRY, index);
	tcapi_set(nodeName, "Enable", "No");
	tcapi_set(nodeName, OMCI_SIP_DISP_NAME, "");
	tcapi_set(nodeName, OMCI_SIP_AUTH_USER_NAME, "");
	tcapi_set(nodeName, OMCI_SIP_PASSWORD, "");
	tcapi_set(nodeName, OMCI_SIP_URI, "");
	tcapi_set(nodeName, OMCI_SIP_USER_PART_AOR, "");
	if( 0 != tcapi_commit(nodeName) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_commit %s fail.\n",__FILE__, __LINE__,nodeName);
		return -1;
	}

	tcapi_set(OMCI_VOIP_ADVANCED_COMMON, OMCI_SIP_VOICEMAIL_EXPIRE, "");
	if( 0 != tcapi_commit(OMCI_VOIP_ADVANCED_COMMON) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_commit OMCI_VOIP_ADVANCED_COMMON fail.\n",__FILE__, __LINE__);
		return -1;
	}

	return 0;
}

/********************************************************************************************************
9.9.3 sip agent config data
*********************************************************************************************************/
int omci_voip_get_reg_exp_time(char *buf, uint8_t buf_size)
{
	if(buf == NULL){
		return -1;
	}
    return tcapi_nget(OMCI_VOIP_ADVANCED_COMMON, "RegistrationExpire", buf, buf_size);
}
int omci_voip_get_re_reg_time(char *buf, uint8_t buf_size)
{
	if(buf == NULL){
		return -1;
	}
    return  tcapi_nget(OMCI_VOIP_ADVANCED_COMMON, "RegisterRetryInterval", buf, buf_size);
}
int omci_voip_get_sip_status(char *buf, uint8_t buf_size, uint8_t index)
{
    char nodeName[30];

	if(buf == NULL){
		return -1;
	}
	snprintf(nodeName, 30, "%s%d",OMCI_VOIP_ADVANCED_ENTRY, index);
    return tcapi_nget(nodeName, "status", buf, buf_size);
}

int omci_voip_set_proxy_enable(char *buf)
{
	if(buf == NULL){
		return -1;
	}
    tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPProxyEnable", "Yes");
	return tcapi_commit(OMCI_VOIP_BASIC_COMMON);
}

// parse the URL string and get the addr and port
static int  omci_voip_parse_url(IN_OUT char * buff /*Careful! Crash if NULL*/,  OUT char **  addr, OUT char ** port)
{
    OMCI_FUNC_TRACE_ENTER;

	if(buff == NULL || addr == NULL || port == NULL){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: ERROR:parameter is NULL!\n",__FUNCTION__,__LINE__);
		return -1;
	}
    omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "[%s][%d]: String to Parse: %s\n", __FUNCTION__,__LINE__,buff);
    
    *addr = buff;
    *port = NULL;
    
    char * delimter_pos = strchr(buff, ':');

    if ( NULL != delimter_pos )
    {
        * delimter_pos = '\0';
        * port =  ++delimter_pos;
    }
	
    OMCI_FUNC_TRACE_LEAVE;
	return 0;
}

int omci_voip_set_proxy_server_addr(char *buf)
{
	char *addr = NULL,  *port = NULL;

	if(buf == NULL){
		return -1;
	}
	if(omci_voip_parse_url(buf, &addr, &port) != 0)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: omci_voip_parse_url fail!\n",__FUNCTION__,__LINE__);
        return -1;
	}
    if ( 0 != tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPProxyAddr", addr) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_set addr fail! \n",__FUNCTION__,__LINE__);
        return -1;
    }

    if (NULL != port){
        if ( 0 != tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPProxyPort", port) ){
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_set port fail! \n",__FUNCTION__,__LINE__);
			return -1;
        }
    }
    
    if( 0 != tcapi_commit(OMCI_VOIP_BASIC) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_commit fail! \n",__FUNCTION__,__LINE__);
        return -1;
    }
    return 0;
}
int omci_voip_set_out_proxy_addr(char *buf)
{
	char *addr = NULL,  *port = NULL;

	if(buf == NULL){
		return -1;
	}
	if(omci_voip_parse_url(buf, &addr, &port) != 0)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: omci_voip_parse_url fail!\n",__FUNCTION__,__LINE__);
        return -1;
	}
    if ( 0 != tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPOutboundProxyAddr", addr) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_set addr fail! \n",__FUNCTION__,__LINE__);
        return -1;
    }

    if (NULL != port){
        if ( 0 != tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPOutboundProxyPort", port) ){
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_set port fail! \n",__FUNCTION__,__LINE__);
			return -1;
        }
    }
    
    if( 0 != tcapi_commit(OMCI_VOIP_BASIC) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_commit fail! \n",__FUNCTION__,__LINE__);
        return -1;
    } 
	return 0;
}
int omci_voip_set_reg_exp_time(char *buf)
{
	if(buf == NULL){
		return -1;
	}
    tcapi_set(OMCI_VOIP_ADVANCED_COMMON, "RegistrationExpire", buf);
    return tcapi_commit(OMCI_VOIP_ADVANCED);
}
int omci_voip_set_re_reg_time(char *buf)
{
	if(buf == NULL){
		return -1;
	}
	tcapi_set(OMCI_VOIP_ADVANCED_COMMON, "RegisterRetryInterval", buf);
    return tcapi_commit(OMCI_VOIP_ADVANCED);
}
int omci_voip_set_sip_reg(char *buf)
{
	char *addr = NULL,  *port = NULL;

	if(buf == NULL){
		return -1;
	}
    if(omci_voip_parse_url(buf, &addr, &port) != 0)
    {
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: omci_voip_parse_url fail!\n",__FUNCTION__,__LINE__);
        return -1;
	}

    if ( 0 != tcapi_set(OMCI_VOIP_BASIC_COMMON, "RegistrarServer", addr)){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_set RegistrarServer fail.\n",__FILE__, __LINE__);
        return -1;
    }

    if (NULL != port){
        if ( 0 != tcapi_set(OMCI_VOIP_BASIC_COMMON, "RegistrarServerPort", port) ){
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_set RegistrarServerPort fail.\n",__FILE__, __LINE__);
			return -1;
        }
    }

    if( 0 != tcapi_commit(OMCI_VOIP_BASIC) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_commit OMCI_VOIP_BASIC fail.\n",__FILE__, __LINE__);
        return -1;
    }
    return 0;
}

int omci_voip_reset_sip_agent_cfg(void)
{
	tcapi_set(OMCI_VOIP_BASIC_COMMON, "RegistrationExpire", "3600");
	tcapi_set(OMCI_VOIP_BASIC_COMMON, "RegistrarServer", "");
	tcapi_set(OMCI_VOIP_BASIC_COMMON, "RegistrarServerPort", "5060");
	tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPProxyAddr", "");
	tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPProxyPort", "5060");
	tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPOutboundProxyAddr", "");
	tcapi_set(OMCI_VOIP_BASIC_COMMON, "SIPOutboundProxyPort", "5060");

	if( 0 != tcapi_commit(OMCI_VOIP_BASIC) ){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: tcapi_commit OMCI_VOIP_BASIC fail.\n",__FILE__, __LINE__);
		return -1;
	}

	return 0;
}

/********************************************************************************************************
9.9.5 voip media profile
*********************************************************************************************************/
int omci_voip_get_fax_mode(char *buf, uint8_t buf_size)
{
	if(buf == NULL){
		return -1;
	}
    return tcapi_nget(OMCI_VOIP_MEDIA_COMMON, OMCI_FAX_CODEC_NAME, buf, buf_size);
}
int omci_voip_get_codec_select(char *buf, uint8_t buf_size, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};

	if(buf == NULL){
		return -1;
	}

	sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY, index);

    return tcapi_nget(nodeName, OMCI_MEDIA_CODEC_NAME, buf, buf_size);
}
int omci_voip_get_pack_period_select(char *buf, uint8_t buf_size, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	uint8_t sipPTimeFlag = 0;
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	
	if(buf == NULL){
		return -1;
	}
	
    if(tcapi_nget("VoIPAdvanced_Common", "SIPPTimeSepEnable", tempBuffer, sizeof(tempBuffer)) == 0){
        sipPTimeFlag = atoi(tempBuffer);
    }
    else{
        sipPTimeFlag = 0;
    }
    if(sipPTimeFlag == 0)
	    sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC, 0);
    else
	    sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY,index);

    return tcapi_nget(nodeName, OMCI_MEDIA_SIP_PACKETIZATION_TIME_NAME, buf, buf_size);
}
int omci_voip_get_silence_suppress(uint8_t *silence_flag, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	
	if(silence_flag == NULL){
		return -1;
	}
#if defined(TCSUPPORT_CT)
	sprintf(nodeName, "%s%d", OMCI_VOIP_MEDIA_ENTRY, 0);
	memset(tempBuffer, 0, MAX_BUFFER_SIZE);
	if(tcapi_nget(nodeName, OMCI_MEDIA_SILENCE_COMPRESSIONENABLE, tempBuffer, sizeof(tempBuffer)) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:tcapi_nget fail !\n",__FUNCTION__,__LINE__);
		return -1;
	}
	
	if(strcmp(tempBuffer,OMCI_MEDIA_SILENCE_ENABLE) == 0){
		*silence_flag = OMCI_MEDIA_SILENCE_ON;
	}else{
		*silence_flag = OMCI_MEDIA_SILENCE_OFF;
	}
#else
	switch(index){
		case OMCI_MEDIA_1ST:
		case OMCI_MEDIA_2ND:
		case OMCI_MEDIA_3RD:
		case OMCI_MEDIA_4TH:
			sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY,index);
			break;
		default:
			return -1;
	}
	memset(tempBuffer, 0, MAX_BUFFER_SIZE);
	if(tcapi_nget(nodeName, OMCI_MEDIA_SILENCE_SUPPRESSION_NAME, tempBuffer, sizeof(tempBuffer)) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:tcapi_nget fail !\n",__FUNCTION__,__LINE__);
		return -1;
	}
	
	if(strcmp(tempBuffer,OMCI_MEDIA_VAD_ON_VAL) == 0){
		*silence_flag = OMCI_MEDIA_SILENCE_ON;
	}else{
		*silence_flag = OMCI_MEDIA_SILENCE_OFF;
	}
#endif	
	return 0;
}
int omci_voip_get_oob_dtmf(char *buf, uint8_t buf_size)
{
	if(buf == NULL){
		return -1;
	}
    return tcapi_nget(OMCI_VOIP_ADVANCED_COMMON, OMCI_MEDIA_DTMF_MODE_NAME, buf, buf_size);
}

int omci_voip_set_fax_mode(char *buf)
{
	if(buf == NULL){
		return -1;
	}
	tcapi_set(OMCI_VOIP_MEDIA_COMMON, OMCI_FAX_CODEC_NAME, buf);
    return tcapi_commit(OMCI_VOIP_MEDIA_COMMON);
}

#if defined(TCSUPPORT_CT)	
static int omci_voip_get_codec_entry_index(uint8_t codec_type)
{
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	uint8_t tmp_codec_type = 0;
	int i = 0;
	
	for(i=0; i<OMCI_MEDIA_NUM; i++)
	{
		memset(tempBuffer, 0, MAX_BUFFER_SIZE);
		if(omci_voip_get_codec_select(tempBuffer, sizeof(tempBuffer), i) != 0){
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:get codec select fail!\n",__FUNCTION__,__LINE__);
			return -1;
		}
		if(strcmp(tempBuffer,OMCI_MEDIA_SIPSUPPORTED_CODECS_PCMA) == 0){
			tmp_codec_type = OMCI_MEDIA_CODEC_PCMA;
		}else if(strcmp(tempBuffer,OMCI_MEDIA_SIPSUPPORTED_CODECS_PCMU) == 0){
			tmp_codec_type = OMCI_MEDIA_CODEC_PCMU;
		}else if(strcmp(tempBuffer,OMCI_MEDIA_SIPSUPPORTED_CODECS_G722) == 0){
			tmp_codec_type = OMCI_MEDIA_CODEC_G722;
		}else if(strcmp(tempBuffer,OMCI_MEDIA_SIPSUPPORTED_CODECS_G723) == 0){
			tmp_codec_type = OMCI_MEDIA_CODEC_G723;	
		}else if(strstr(tempBuffer,OMCI_MEDIA_SIPSUPPORTED_CODECS_G726) != NULL){
			tmp_codec_type = OMCI_MEDIA_CODEC_G726;
		}else if(strcmp(tempBuffer,OMCI_MEDIA_SIPSUPPORTED_CODECS_G729) == 0){
			tmp_codec_type = OMCI_MEDIA_CODEC_G729;
		}else{
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]: invalid tempBuffer=%s\n",__FUNCTION__,__LINE__,tempBuffer);
			return -1;
		}
		omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "[%s][%d]: tmp_codec_type=%d and codec_type=%d\n",__FUNCTION__,__LINE__,tmp_codec_type,codec_type);
		if(tmp_codec_type == codec_type){
			omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "[%s][%d]: i=%d\n",__FUNCTION__,__LINE__,i);
			break;
		}
	}
	return i;
}
	
static void reset_same_pri_voip_codecs(uint8_t input_pri, uint8_t save_pri, uint8_t index)
{
    int i = 0;
    char nodeName[MAX_BUFFER_SIZE] = {0};   
    char tempBuffer[MAX_BUFFER_SIZE] = {0};
    char str_pri[MAX_BUFFER_SIZE] = {0};
    
    for(i=0; i<OMCI_MEDIA_NUM; i++)
    {
        if(index == i)
            continue;
		memset(nodeName, 0, sizeof(nodeName));
		memset(tempBuffer, 0, sizeof(tempBuffer));
		sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY, i);
		if(tcapi_nget(nodeName, OMCI_MEDIA_PRIORITY,tempBuffer, sizeof(tempBuffer)) != 0){
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:tcapi_nget fail !\n",__FUNCTION__,__LINE__);
			return;
		}
		if(input_pri == atoi(tempBuffer)){
		    sprintf(str_pri, "%d", save_pri);
    		tcapi_set(nodeName, OMCI_MEDIA_PRIORITY, str_pri);
    		tcapi_set(nodeName, OMCI_MEDIA_ENABLE, "No");
    		break;
        }
	}
}
#endif
int omci_voip_set_codec_select(uint8_t codec_type, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	int ret = -1;
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
#if defined(TCSUPPORT_CT)	
	int codec_index = 0;
	char index_priBuff[MAX_BUFFER_SIZE] = {0};
	uint8_t index_pri = 0;
	uint8_t save_pri = 0;

	memset(index_priBuff, 0, MAX_BUFFER_SIZE);
	switch(index){
		case OMCI_MEDIA_1ST:
		case OMCI_MEDIA_2ND:		
		case OMCI_MEDIA_3RD:		
		case OMCI_MEDIA_4TH:
			index_pri = index + 1;
			sprintf(index_priBuff, "%d", index_pri);
			break;	
		default:
			goto end;
	}
	codec_index = omci_voip_get_codec_entry_index(codec_type);
	if((codec_index >= OMCI_MEDIA_NUM) || (codec_index < 0)){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:i=%d!\n",__FUNCTION__,__LINE__,codec_index);
		goto end;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY, codec_index);
	memset(tempBuffer, 0, sizeof(tempBuffer));
	if(tcapi_nget(nodeName, OMCI_MEDIA_PRIORITY, tempBuffer,sizeof(tempBuffer)) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:tcapi_nget fail !\n",__FUNCTION__,__LINE__);
		goto end;
	}
	save_pri = atoi(tempBuffer);
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "[%s][%d]: tcapi set[%s] priority[%s], save_pri[%d].\n",__FUNCTION__,__LINE__,nodeName,index_priBuff,save_pri);
	tcapi_set(nodeName, OMCI_MEDIA_PRIORITY, index_priBuff);
	tcapi_set(nodeName, OMCI_MEDIA_ENABLE, "Yes");
	reset_same_pri_voip_codecs(index_pri,save_pri, codec_index);
#else
	switch(index){
		case OMCI_MEDIA_1ST:
		case OMCI_MEDIA_2ND:
		case OMCI_MEDIA_3RD:
		case OMCI_MEDIA_4TH:
			sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY,index);
			break;
		default:
			goto end;
	}
	
	memset(tempBuffer, 0, MAX_BUFFER_SIZE);
	switch(codec_type){
		case OMCI_MEDIA_CODEC_PCMA:
			strcpy(tempBuffer, OMCI_MEDIA_SIPSUPPORTED_CODECS_PCMA);
			break;
		case OMCI_MEDIA_CODEC_PCMU:
			strcpy(tempBuffer, OMCI_MEDIA_SIPSUPPORTED_CODECS_PCMU);
			break;
		case OMCI_MEDIA_CODEC_G722:
			strcpy(tempBuffer, OMCI_MEDIA_SIPSUPPORTED_CODECS_G722);
			break;
		case OMCI_MEDIA_CODEC_G723:
			strcpy(tempBuffer, OMCI_MEDIA_SIPSUPPORTED_CODECS_G723);
			break;
		case OMCI_MEDIA_CODEC_G726:
			strcpy(tempBuffer, OMCI_MEDIA_SIPSUPPORTED_CODECS_G726);
			break;
		case OMCI_MEDIA_CODEC_G729:
			strcpy(tempBuffer, OMCI_MEDIA_SIPSUPPORTED_CODECS_G729);
			break;
		default:
			goto end;
	}
	
	tcapi_set(nodeName, OMCI_MEDIA_CODEC_NAME, tempBuffer);
#endif
	
	if(tcapi_commit(nodeName) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:tcapi_commit fail !\n",__FUNCTION__,__LINE__);
		goto end;
	}
	ret  = 0;
end:
    return ret;
}

int omci_voip_set_pack_period_select(uint8_t codec_type,char *buf, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	uint8_t sipPTimeFlag = 0;
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	int codec_index = 0;
	
	if(buf == NULL){
		return -1;
	}
	codec_index = omci_voip_get_codec_entry_index(codec_type);
	if((codec_index >= OMCI_MEDIA_NUM) || (codec_index < 0)){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:i=%d!\n",__FUNCTION__,__LINE__,codec_index);
		return -1;
	}
    if(tcapi_nget("VoIPAdvanced_Common", "SIPPTimeSepEnable", tempBuffer, sizeof(tempBuffer)) == 0){
        sipPTimeFlag = atoi(tempBuffer);
    }
    else{
        sipPTimeFlag = 0;
    }
    if(sipPTimeFlag == 0)
	    sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC, 0);
    else
    {
 		sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY,codec_index);
    }
	tcapi_set(nodeName, OMCI_MEDIA_SIP_PACKETIZATION_TIME_NAME, buf);
	if(tcapi_commit(nodeName) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]:tcapi_commit fail !\n",__FUNCTION__,__LINE__);
		return -1;
	}
    return 0;
}
int omci_voip_set_silence_suppress(uint8_t silence_flag, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char tempBuffer[MAX_BUFFER_SIZE] = {0};

#if defined(TCSUPPORT_CT)
	sprintf(nodeName, "%s%d", OMCI_VOIP_MEDIA_ENTRY, 0);
	switch(silence_flag){
		case OMCI_MEDIA_SILENCE_OFF:
			strcpy(tempBuffer,OMCI_MEDIA_SILENCE_DISABLE);
			break;		
		case OMCI_MEDIA_SILENCE_ON:
			strcpy(tempBuffer,OMCI_MEDIA_SILENCE_ENABLE);
			break;
		default:
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d] silence_flag=%d(0 or 1),error\n",__FUNCTION__, __LINE__, silence_flag);
			return -1;
			
	}
	tcapi_set(nodeName, OMCI_MEDIA_SILENCE_COMPRESSIONENABLE, tempBuffer);
#else
	switch(index){
		case OMCI_MEDIA_1ST:			
		case OMCI_MEDIA_2ND:
		case OMCI_MEDIA_3RD:
		case OMCI_MEDIA_4TH:
			sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY,index);
			break;
		default:
			omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d] silence_flag=%d(0 or 1),error\n",__FUNCTION__, __LINE__, silence_flag);
			return -1;
	}
	
	switch(silence_flag){
		case OMCI_MEDIA_SILENCE_OFF:
			strcpy(tempBuffer,OMCI_MEDIA_VAD_OFF_VAL);
			break;
		case OMCI_MEDIA_SILENCE_ON:
			strcpy(tempBuffer,OMCI_MEDIA_VAD_ON_VAL);
			break;
		default:
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d] silenceFlag=%d(0 or 1),error\n",__FUNCTION__, __LINE__,silence_flag);
		return -1;
			
	}
	tcapi_set(nodeName, OMCI_MEDIA_SILENCE_SUPPRESSION_NAME, tempBuffer);
#endif
		
	return tcapi_commit(nodeName);
}
int omci_voip_set_oob_dtmf(char *buf)
{
	if(buf == NULL){
		return -1;
	}
	tcapi_set(OMCI_VOIP_ADVANCED_COMMON, OMCI_MEDIA_DTMF_MODE_NAME, buf);
	return tcapi_commit(OMCI_VOIP_ADVANCED_COMMON);
}

/*******************************************************************************************************************************
9.9.10 network dial plan table

********************************************************************************************************************************/
int omci_voip_set_digital_map(char *buf, uint8_t index)
{
    char dialMap[32] = {0};

	if(buf == NULL){
		return -1;
	}
	sprintf(dialMap,"DigitMap%d",index);
	
	return tcapi_set(OMCI_VOIP_DIGIT_MAP_ENTRY, dialMap, buf);
}

/********************************************************************************************************
9.9.11 voip line status
*********************************************************************************************************/
int omci_voip_get_codec_used(uint8_t *index_pri, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char index_priBuff[MAX_BUFFER_SIZE] = {0};

	if(index_pri == NULL){
		return -1;
	}
	sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC_ENTRY,index);
    if(tcapi_nget(nodeName, OMCI_MEDIA_PRIORITY, index_priBuff, MAX_BUFFER_SIZE) != 0){
		return -1;
    }
	*index_pri = atoi(index_priBuff);
	
	return 0;
}
int omci_voip_get_voice_server_status(omci_voip_ser_status_t *ser_status, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	char temp2Buffer[MAX_BUFFER_SIZE] = {0};
	
	if(ser_status == NULL){
		return -1;
	}
    sprintf(nodeName, "InfoVoIP_Entry%d", index);
	if(0 != tcapi_nget(nodeName, "Status", tempBuffer, MAX_BUFFER_SIZE)){
		  return -1;
	}
	sprintf(nodeName, "VoIPSysParam_Entry%d", index);
	if(tcapi_nget(nodeName, "SC_LINE_INFO_STATUS", temp2Buffer,MAX_BUFFER_SIZE) != 0){
		return -1;
	}

	if(strcmp(tempBuffer,"Up") == 0){
		if(strcmp(temp2Buffer,"Connect") == 0){
			*ser_status = OMCI_VOIP_SERVER_STATUS_IN_SESSION;
		}else{
			*ser_status = OMCI_VOIP_SERVER_STATUS_REGISTERED;
		}
	}else if(strcmp(tempBuffer,"Error") == 0){
		*ser_status = OMCI_VOIP_SERVER_STATUS_FAILED;
	}else if(strcmp(tempBuffer,"Initializing") == 0){
		*ser_status = OMCI_VOIP_SERVER_STATUS_INITIAL;	
	}else{
		*ser_status = OMCI_VOIP_SERVER_STATUS_NOTCONFIG;
	}
	return 0;
}
int omci_voip_get_port_session_type(omci_voip_port_ses_type_t *port_sess_type, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	
	if(port_sess_type == NULL){
		return -1;
	}
    sprintf(nodeName, OMCI_VOIP_SYS_PARAM_ENTRY, index);

	if(0 != tcapi_nget(nodeName, "SC_LINE_SESS_STATE", tempBuffer, MAX_BUFFER_SIZE)){
		  return -1;
	}
	
	if(strcmp(tempBuffer,"Idle") == 0){
		*port_sess_type = OMCI_VOIP_PORT_SESSION_TYPE_IDLE;
	}else if(strcmp(tempBuffer,"2way") == 0){
		*port_sess_type = OMCI_VOIP_PORT_SESSION_TYPE_2WAY;
	}else if(strcmp(tempBuffer,"3way") == 0){
		*port_sess_type = OMCI_VOIP_PORT_SESSION_TYPE_3WAY;
	}else if(strcmp(tempBuffer,"Fax") == 0){
		*port_sess_type = OMCI_VOIP_PORT_SESSION_TYPE_FAX;
	}else if(strcmp(tempBuffer,"Telemetry") == 0){
		*port_sess_type = OMCI_VOIP_PORT_SESSION_TYPE_TELEMETRY;
	}else if(strcmp(tempBuffer,"Conference") == 0){
		*port_sess_type = OMCI_VOIP_PORT_SESSION_TYPE_CONFERENCE;
	}else{
		*port_sess_type = OMCI_VOIP_PORT_SESSION_TYPE_IDLE;
	}
	return 0;
}

int omci_voip_get_device_status(omci_voip_dev_status_t *dev_status, uint8_t index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	char tempBuffer2[MAX_BUFFER_SIZE] = {0};

	if(dev_status == NULL){
		return -1;
	}
	if(0 != tcapi_nget(OMCI_VOIP_BASIC_COMMON, "SIPProtocol", tempBuffer, MAX_BUFFER_SIZE)){
		  return -1;
	}

	if(!strcmp(tempBuffer, "H.248"))	/*H.248 protocal*/
	{
		sprintf(nodeName, OMCI_VOIP_INFO_H248_ENTRY, index);
		if(0 != tcapi_nget(nodeName,"lineInfoStatus", tempBuffer2, MAX_BUFFER_SIZE)){
			return -1;
		}
	}
	else	/*SIP protocal*/
	{
		sprintf(nodeName, OMCI_VOIP_SYS_PARAM_ENTRY, index);
		if(0 != tcapi_nget(nodeName, "SC_LINE_INFO_STATUS", tempBuffer2, MAX_BUFFER_SIZE)){
			return -1;
		}
	}
	
	if(strcmp(tempBuffer2, "Idle")){
		*dev_status = VOIP_DEVICE_BUSY;
	}else{
		*dev_status = VOIP_DEVICE_IDLE;
	}
	
	return 0;
}

int omci_voip_get_packet_period(uint16_t *pkt_period, omci_voip_pkt_period_t period_index)
{
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	
	if(pkt_period == NULL){
		return -1;
	}
#if defined(TCSUPPORT_CT)
	sprintf(nodeName, "%s%d", OMCI_VOIP_CODECS_PVC, period_index-1);
	if(tcapi_nget(nodeName,"SIPPacketizationTime", tempBuffer,sizeof(tempBuffer)) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d] tcapi_nget VoIPCodecs_PVC0 fail\n",__FUNCTION__,__LINE__);
		return -1;
	}
#else
	sprintf(nodeName, "%s%d", OMCI_VOIP_ADVANCED_ENTRY, period_index-1);
	if(tcapi_nget(nodeName,"SIPPacketizationTime", tempBuffer,sizeof(tempBuffer)) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d] tcapi_nget VoIPAdvanced_Entry0 fail\n",__FUNCTION__,__LINE__);
		return -1;
	}
#endif
	*pkt_period = atoi(tempBuffer);

    return 0;
}

int omci_voip_get_line_state(omci_voip_line_state_t *line_state, uint8_t index)
{
	omci_voip_line_state_t status = OMCI_VOIP_LINE_HOOK_STATE_TYPE_UNKNOWN;
	char nodeName[MAX_BUFFER_SIZE] = {0};
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	
	memset(tempBuffer, 0, MAX_BUFFER_SIZE);
	sprintf(nodeName, "%s%d", "VoIPSysParam_Entry", index);
	if(tcapi_nget(nodeName, "SC_LINE_INFO_STATUS", tempBuffer,sizeof(tempBuffer)) != 0){
		omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d] tcapi_nget VoIPCodecs_PVC0 fail\n",__FUNCTION__,__LINE__);
		return -1;
	}
	if(strcmp(tempBuffer,"Idle") == 0){
		status = OMCI_VOIP_LINE_HOOK_STATE_TYPE_IDLE;
	}else if(strcmp(tempBuffer,"Dialing") == 0){
		status = OMCI_VOIP_LINE_HOOK_STATE_TYPE_DIALLING;
	}else if(strcmp(tempBuffer,"Ringing") == 0){
		status = OMCI_VOIP_LINE_HOOK_STATE_TYPE_RINGING;
	}else if(strcmp(tempBuffer,"Ringback") == 0){
		status = OMCI_VOIP_LINE_HOOK_STATE_TYPE_RINGBACK;
	}else if(strcmp(tempBuffer,"Connect") == 0){
		status = OMCI_VOIP_LINE_HOOK_STATE_TYPE_CONNECTED;
	}else if(strcmp(tempBuffer,"Disconnect") == 0){
		status = OMCI_VOIP_LINE_HOOK_STATE_TYPE_DISCONNECT;
	}
	*line_state = status;
	return 0;
}


/********************************************************************************************************
9.9.16 mgc config data
*********************************************************************************************************/
int omci_voip_get_term_id_base(char *buf, uint8_t buf_size)
{
	if(buf == NULL){
		return -1;
	}
    return tcapi_nget(OMCI_VOIP_H248_COMMON, OMCI_H248_DEVICE_ID_NAME, buf, buf_size);
}

int omci_voip_set_mgc_info(char *buf, uint8_t index)
{
	char attrStr[MAX_BUFFER_SIZE] = {0};	
	char attrStr2[MAX_BUFFER_SIZE] = {0};
	char tmpBuff[MAX_BUFFER_SIZE] = {0};
	char * tmpStr_ptr = NULL;
	uint32_t mgcPort = 0;

	if(buf == NULL){
		return -1;
	}
	if(index == OMCI_H248_PRIMARY_ATTR_INDEX){
		strcpy(attrStr,OMCI_H248_PRIMARY_MGC_IP_NAME);
		strcpy(attrStr2,OMCI_H248_PRIMARY_MGC_PORT_NAME);		
	}else if(index == OMCI_H248_SECNDARY_ATTR_INDEX){
		strcpy(attrStr,OMCI_H248_SECONDARY_MGC_IP_NAME);
		strcpy(attrStr2,OMCI_H248_SECONDARY_MGC_PORT_NAME);
	}else{
		//nothing
	}

	if(strstr((char *)buf, ":") == NULL){
		/*set primary or secondary mgc ip*/
		tcapi_set(OMCI_VOIP_H248_COMMON, attrStr, (char *)buf);
		/*set primary or secondary mgc port*/
		tcapi_set(OMCI_VOIP_H248_COMMON, attrStr2, "");
	}else{
		tmpStr_ptr = strtok((char *)buf, ":");
		/*set primary or secondary mgc ip*/
		tcapi_set(OMCI_VOIP_H248_COMMON, attrStr, tmpStr_ptr);

		/*set primary or secondary mgc port*/
		tmpStr_ptr = strtok(NULL, ":");
		if(tmpStr_ptr != NULL){
			mgcPort = atoi(tmpStr_ptr);
			memset(tmpBuff, 0, sizeof(tmpBuff));
			if(mgcPort != 0){
				sprintf(tmpBuff, "%d", mgcPort);
			}			
			tcapi_set(OMCI_VOIP_H248_COMMON, attrStr2, tmpBuff);
		}else{
			tcapi_set(OMCI_VOIP_H248_COMMON, attrStr2, "");
		}
	}
	return tcapi_commit(OMCI_VOIP_H248_COMMON);
}

int omci_voip_set_term_id_base(char *term_id_base)
{
	if(term_id_base == NULL){
		return -1;
	}
    tcapi_set(OMCI_VOIP_H248_COMMON, OMCI_H248_DEVICE_ID_NAME, term_id_base);
	return tcapi_commit(OMCI_VOIP_H248_COMMON);
}

/********************************************************************************************************
9.9.18 voip config data
*********************************************************************************************************/
int omci_voip_get_avail_signal_protoc(uint8_t *protocol)
{
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	if(protocol == NULL){
		return -1;
	}
    if(tcapi_nget(OMCI_VOIP_ADVANCED_COMMON, OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_NAME, tempBuffer, sizeof(tempBuffer))){
		return -1;
	}
	if(strstr(tempBuffer, OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_SIP_VAL) != NULL){
		*protocol |= OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_SIP;
	}
	if(strstr(tempBuffer, OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_H248_VAL) != NULL){
		*protocol |= OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_H248;
	}
	if(strstr(tempBuffer, OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_MGCP_VAL) != NULL){
		*protocol |= OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_MGCP;
	}
	return 0;
}
int omci_voip_get_signal_protocol_used(uint8_t *protocol)
{
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	if(protocol == NULL){
		return -1;
	}
	if(tcapi_nget(OMCI_VOIP_DIGIT_MAP_ENTRY, OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_NAME, tempBuffer, sizeof(tempBuffer))){
		return -1;
	}

	if(strstr(tempBuffer, OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_SIP_VAL) != NULL){
		*protocol = OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_SIP;
	}else if(strstr(tempBuffer, OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_H248_VAL) != NULL){
		*protocol = OMCI_VOIP_CFG_DATA_PTOCOLS_USED_H248;
	}else if(strstr(tempBuffer, OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_MGCP_VAL) != NULL){
		*protocol = OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_MGCP;
	}else{
		*protocol = OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_NONE;
	}
    return 0;
}
int omci_voip_get_profile_version(char *version, uint8_t version_len)
{
	if(version == NULL){
		return -1;
	}
    return tcapi_nget(OMCI_VOIP_DIGIT_MAP_ENTRY, OMCI_VOIP_CFG_DATA_PROFILE_VERSION_NAME, version, version_len);
}

int omci_voip_set_signal_protocol_used(uint8_t protocol)
{
	char tempBuffer[MAX_BUFFER_SIZE] = {0};
	switch(protocol){
		case OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_SIP:
			strcpy(tempBuffer,OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_SIP_VAL);
			break;
		case OMCI_VOIP_CFG_DATA_PTOCOLS_USED_H248:
			strcpy(tempBuffer,OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_H248_VAL);
			break;
		case OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_MGCP:
			strcpy(tempBuffer,OMCI_VOIP_CFG_DATA_SIGNALING_PROTOCOLS_MGCP_VAL);
			break;
		case OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_NONE:			
			break;
		case OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_NO_OMCI:
			return 0;
			break;
		default:
			return -1;
	}
	tcapi_set(OMCI_VOIP_DIGIT_MAP_ENTRY, OMCI_VOIP_CFG_DATA_PROTOCOLS_USED_NAME, tempBuffer);
	return tcapi_commit(OMCI_VOIP_DIGIT_MAP_ENTRY);
}
int omci_voip_set_config_method_used(uint8_t cfg_method)
{
	char tempBuffer[MAX_BUFFER_SIZE] = {0};

	switch(cfg_method){
		case OMCI_VOIP_CFG_DATA_CFG_METHOD_NOT_USED:
			strcpy(tempBuffer,OMCI_VOIP_CFG_DATA_CFG_METHOD_NOTCFG_VAL);
			break;
		case OMCI_VOIP_CFG_DATA_CFG_METHOD_USED_OMCI:
			strcpy(tempBuffer,OMCI_VOIP_CFG_DATA_CFG_METHOD_OMCI_VAL);
			break;
		case OMCI_VOIP_CFG_DATA_CFG_METHOD_USED_TR69:
			strcpy(tempBuffer,OMCI_VOIP_CFG_DATA_CFG_METHOD_TR69_VAL);
			break;
		default:
			return -1;
	}
	tcapi_set(OMCI_VOIP_BASIC_COMMON, OMCI_VOIP_CFG_DATA_CFG_METHOD_NAME, tempBuffer);
	return tcapi_commit(OMCI_VOIP_BASIC_COMMON);
}

uint8_t omci_voip_get_config_method_used(void)
{
    uint8_t cfg_method = 0;
    char tempBuffer[MAX_BUFFER_SIZE] = {0};
    int ret = -1;

	ret = tcapi_nget(OMCI_VOIP_BASIC_COMMON, OMCI_VOIP_CFG_DATA_CFG_METHOD_NAME, tempBuffer, MAX_BUFFER_SIZE);
    if (0 != ret)
        return 0xFF;

    if (0 == strcmp(tempBuffer,OMCI_VOIP_CFG_DATA_CFG_METHOD_NOTCFG_VAL)){
		cfg_method = OMCI_VOIP_CFG_DATA_CFG_METHOD_NOT_USED;
    } else if (0 == strcmp(tempBuffer,OMCI_VOIP_CFG_DATA_CFG_METHOD_OMCI_VAL)){
			cfg_method = OMCI_VOIP_CFG_DATA_CFG_METHOD_USED_OMCI;
    } else if (0 == strcmp(tempBuffer,OMCI_VOIP_CFG_DATA_CFG_METHOD_TR69_VAL)){
			cfg_method = OMCI_VOIP_CFG_DATA_CFG_METHOD_USED_TR69;
    } else {
        return 0xFF;
    }

	return cfg_method;
}

int omci_is_voip_related_me(uint16_t class_id){
    char nodeName[MAX_BUFFER_SIZE] = {0};
    char tempBuffer[MAX_BUFFER_SIZE] = {0};
	if (class_id == OMCI_CLASS_ID_PPTP_POTS_UNI || class_id == OMCI_CLASS_ID_SIP_USER_DATA 
        || class_id == OMCI_CLASS_ID_SIP_AGENT_CONFIG_DATA || class_id == OMCI_CLASS_ID_VOIP_VOICE_CTP
		|| class_id == OMCI_CLASS_ID_VOIP_MEDIA_PROFILE || class_id == OMCI_CLASS_ID_NETWORK_DIAL_PLAN_TBL 
        || class_id == OMCI_CLASS_ID_MGC_CFG_DATA || class_id == OMCI_CLASS_ID_VOIP_LINE_STATUS)
    {
        strncpy(nodeName,OMCI_VOIP_BASIC_COMMON,MAX_BUFFER_SIZE-1);
        
        if((tcapi_get(nodeName, OMCI_VOIP_CFG_DATA_CFG_METHOD_NAME, tempBuffer)) != 0){
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR,"[%s]:tcapi_get fail ![%d]\n",__FUNCTION__,__LINE__);
            return 0;
        }
        if(0 != strcmp(tempBuffer,OMCI_VOIP_CFG_DATA_CFG_METHOD_OMCI_VAL))
        {
            return 1;
        }
    }
	return 0;
}

